﻿<?php
ini_set('date.timezone','Asia/Shanghai');
include 'simple_html_dom.php';
include 'config_catchwandoujia.php';

define('APPS_NUM',2405);
define('GAMES_NUM',977);

define('DBPAS',"coolchuan2012");
//define('DBPAS',"123456");

//coolchuan2012
$link = mysql_connect("localhost","root",DBPAS); 
mysql_select_db("catchappdataall", $link);
mysql_query('SET NAMES UTF8');


define('TYPEURLFOOTER','&os=17&model=HTC+Sensation&cpu=pyramid&order=download');
define('ONCECATCHCOUNT',20);

define('ONCECATCHCOUNTMAX',40);

define('WEBURLHEADER','http://www.wandoujia.com/apps/');

define('LOGPATHHEADER','/var/www/catchjiankongdata/');
//define('LOGPATHHEADER','');

define('LOGFORSUCCESS',0);


define('WAPURLHEADERWANDOUJIA','http://apps.wandoujia.com/api/v1/apps/');
define('WAPURLFOOTERWANDOUJIA','?f=phoenix2&v=3.34.1&u=ecaf666a0f8e4553b843d5e61e80c62ad30247cd&max=20&start=0&opt_fields=apks.compatible%2Capks.incompatibleDetail%2Cad%2Capks.adsType%2Capks.beta%2Capks.bytes%2Capks.downloadUrl.*%2Capks.size%2Capks.versionCode%2Capks.versionName%2Capks.securityStatus%2Capks.md5%2Capks.permissionLevel%2Capks.superior%2Capks.dangerousPermissions%2Capks.permissions%2Capks.securityDetail.*%2Capks.resolution%2Ccategories.alias%2Ccategories.name%2Cdescription%2Ctitle%2CcommentsCount%2ClikeCount%2CdislikesCount%2CinstalledCountStr%2Cicons.px78%2CpackageName%2Cid%2ClikesRate%2Cscreenshots.*%2Capks.verified%2Capks.paidType%2CimprUrl%2Cbanner%2Ctags.*&vc=4197&ch=wandoujia_wap&pos=m/tag/shopping');


//getinfoandrank();

getrankfrompc();

//getcomment();


function getinfoandrank()
{
	//获取已存在app基础数据
	
	//应用分类
//	$apptype_arr = getapptypelist();
//	foreach ($apptype_arr as $apptype)
//	{
//		//每一类的url
//		$apps_url_app_type_list = $apptype['url'];
//		decodeurlandgetdata($apptype['id'],"soft",$apps_url_app_type_list);
//	}
//	//应用总榜
//	decodeurlandgetdata(101,"soft","http://apps.wandoujia.com/api/v1/feeds?f=phoenix2&v=3.24.1&vc=3918&u=b19e8780415b44108c0c7f4014898b7b938fd64b&ch=wandoujia_wap&opt_fields=adsCursor,data.app.apks.compatible,data.app.apks.incompatibleDetail,data.reason.*,data.app.title,data.app.packageName,data.app.ad,data.app.icons.px78,data.app.installedCountStr,data.app.apks.downloadUrl.url,data.app.apks.bytes,data.app.apks.verified,data.app.apks.versionName,data.app.apks.versionCode,data.app.detailParam,data.app.imprUrl,data.app.stat.weeklyStr,data.app.exclusiveBadge,data.app.apks.md5,data.app.editorComment,data.app.award.issue,data.app.apks.superior,data.app.apks.paidType,data.app.likesRate&area=index&ads_start=9");
	
	
	//游戏分类
	$gametype_arr = getgametypelist();
	foreach ($gametype_arr as $gametype)
	{
		//每一类的url
		$apps_url_game_type_list = $gametype['url'].TYPEURLFOOTER;
		decodeurlandgetdata($gametype['id'],"game",$apps_url_game_type_list);
	}
	//游戏总榜
	decodeurlandgetdata(102,"game","http://apps.wandoujia.com/api/v1/feeds?f=phoenix2&v=3.24.1&vc=3918&u=b19e8780415b44108c0c7f4014898b7b938fd64b&ch=wandoujia_wap&opt_fields=adsCursor,data.app.apks.compatible,data.app.apks.incompatibleDetail,data.reason.*,data.app.title,data.app.packageName,data.app.ad,data.app.icons.px78,data.app.installedCountStr,data.app.apks.downloadUrl.url,data.app.apks.bytes,data.app.apks.verified,data.app.apks.versionName,data.app.apks.versionCode,data.app.detailParam,data.app.imprUrl,data.app.stat.weeklyStr,data.app.exclusiveBadge,data.app.apks.md5,data.app.editorComment,data.app.award.issue,data.app.apks.superior,data.app.apks.paidType,data.app.likesRate&area=game&ads_start=6");

//	豌豆荚排行榜
//	decodeurlandgetdata(111,"paihang","http://apps.wandoujia.com/api/v1/apps?f=phoenix2&v=3.24.1&vc=3918&u=b19e8780415b44108c0c7f4014898b7b938fd64b&ch=wandoujia_wap&opt_fields=apks.compatible,apks.incompatibleDetail,title,packageName,ad,icons.px78,installedCountStr,apks.downloadUrl.url,apks.bytes,apks.verified,apks.versionName,apks.versionCode,detailParam,imprUrl,stat.weeklyStr,exclusiveBadge,apks.md5,editorComment,apks.paidType,likesRate,apks.superior&type=weeklytop");
//	
//	设计奖
//	decodeurlandgetdata(112,"sheji","http://apps.wandoujia.com/api/v1/apps?type=award&f=phoenix2&opt_fields=apks.compatible%2Capks.incompatibleDetail%2Ctitle%2CpackageName%2Cad%2Cicons.px78%2CinstalledCountStr%2Capks.downloadUrl.url%2Capks.bytes%2Capks.verified%2Capks.versionName%2Capks.versionCode%2CdetailParam%2CimprUrl%2Caward.blogTitle%2Capks.md5%2CeditorComment%2Capks.paidType%2ClikesRate%2Capks.superior&v=3.24.1&u=b19e8780415b44108c0c7f4014898b7b938fd64b&ch=wandoujia_wap");

	catchother();
}




function decodeurlandgetdata($catchtype,$appbigtype,$urlheader)
{
	$app_exit_infos = get_exit_appinfo_fromdb();
	$app_exit_versions = get_exit_appversion_fromdb();
	
	$app_today_data = get_today_data();
	$app_today_score = get_today_score();
	
	for($i=0;;)
	{
		$apps_url_app_all_list = $urlheader."&start=$i&max=".ONCECATCHCOUNT;
	    
		@$json_string = file_get_html($apps_url_app_all_list);
		$obj = json_decode($json_string);
		
		$appinfoarr = $obj;
		
//		if(($appbigtype == "paihang") || (($appbigtype == "sheji")))
//		{
//			//这两类结构直接是data
//			$appinfoarr = $obj;
//		}

		//只有这两个大类排行的接口需要->data
		if((($appbigtype == "soft") || ($appbigtype == "game")) && ($catchtype >= 100))
		{
			$appinfoarr = $obj->data;
		}
		
		//本次为空，结束
		if((count($appinfoarr) == 0) || ($appinfoarr == ""))
		{
			break;
		}
		
		//有内容，解析
		$app_info_arr_insertdb_arr = catchinfodata($catchtype,$appbigtype,$i,$appinfoarr);
		$app_info_arr_insertdb['app_id'] = 0;
		$app_info_arr_insertdb['app_version_id'] = 0;
		
		$app_info_arr_insertdb_data = array();
		$app_info_arr_insertdb_scoredata = array();
		//写数据库
		foreach ($app_info_arr_insertdb_arr as $app_info_arr_insertdb)
		{
			//判断是否需要更新信息库
			$searchinfoid = searchisexit($app_info_arr_insertdb, $app_exit_infos);
			if($searchinfoid == 0)
			{
				$app_info_arr_insertdb['app_id'] = insert_appinfo_ifnotexit_todb($app_info_arr_insertdb);
				$app_info_arr_insertdb['app_version_id'] = insert_appversion_ifnotexit_todb($app_info_arr_insertdb);
				
				//重新获取存在app信息，因为有了更新
				$app_exit_infos = get_exit_appinfo_fromdb();
				$app_exit_versions = get_exit_appversion_fromdb();
			}
			else 
			{
				$app_info_arr_insertdb['app_id'] = $searchinfoid;
				$searchversionid = searchisexitversion($app_info_arr_insertdb, $app_exit_versions);
				if($searchversionid == 0)
				{
					$app_info_arr_insertdb['app_version_id'] = insert_appversion_ifnotexit_todb($app_info_arr_insertdb);
					
					//重新获取存在app信息，因为有了更新
					$app_exit_versions = get_exit_appversion_fromdb();
				}
				else 
				{
					$app_info_arr_insertdb['app_version_id'] = $searchversionid;
				}
			}
			//判断当天没有抓过，再写入
			if(searchisexittodaydata($app_info_arr_insertdb, $app_today_data) == 0)
			{
				//update_appinfo_todb($app_info_arr_insertdb);
				//$app_today_data = get_today_data();
				$app_info_arr_insertdb_data[] = $app_info_arr_insertdb;
			}
			if(searchisexittodayscore($app_info_arr_insertdb, $app_today_score) == 0)
			{
				//update_appscore_todb($app_info_arr_insertdb);
				//$app_today_score = get_today_score();
				$app_info_arr_insertdb_scoredata[] = $app_info_arr_insertdb;
			}

		}
		
		update_appinfo_todb($app_info_arr_insertdb_data);
		update_appscore_todb($app_info_arr_insertdb_scoredata);
//		$app_today_data = get_today_data();
//		$app_today_score = get_today_score();
		$app_info_arr_insertdb_data = array();
		$app_info_arr_insertdb_scoredata = array();
		
		$i = $i + ONCECATCHCOUNT;
		
	//	结束
	//	if($obj->total < ONCECATCHCOUNT)
	//	{
	//		break;
	//	}
	}
}

function searchisexit($app_infos, $app_exit_info) 
{
	foreach ($app_exit_info as $app_exit) 
	{
		if ($app_infos['packagename'] == $app_exit["AppPackageName"]) 
		{
			return $app_exit['id'];
		}
	}
	return 0;
}

function searchisexitversion($app_infos, $app_exit_version) 
{
	foreach ($app_exit_version as $app_exit) 
	{
		if (($app_infos['app_id'] == $app_exit["app_id"]) && ($app_infos['Versioncode'] == $app_exit["versionCode"]) ) 
		{
			return $app_exit['id'];
		}
	}
	return 0;
}

function searchisexittodaydata($app_infos, $app_today_data) 
{
	foreach ($app_today_data as $app_exit) 
	{
		if (($app_infos['app_id'] == $app_exit["app_id"]) && ($app_infos['AppRanking'] == $app_exit["rank"]) && ($app_infos['AppRankingType'] == $app_exit["ranktype"]))
		{
			return $app_exit['id'];
		}
	}
	return 0;
}
function searchisexittodayscore($app_infos, $app_today_score) 
{
	foreach ($app_today_score as $app_exit)
	{
		if (($app_infos['app_id'] == $app_exit["app_id"]))
		{
			return $app_exit['id'];
		}
	}
	return 0;
}



//排名分类，app大类，app信息
function catchinfodata($catchtype,$appbigtype,$flag,$appinfoarr)
{
	$ranking = $flag ;//* ONCECATCHCOUNT;
	$app_info_insertdb_arr = array();
	
	if((($appbigtype == "soft") || ($appbigtype == "game")) && ($catchtype < 100))
	{
		$appinfoarr = $appinfoarr[0]->apps;
	}
	foreach ($appinfoarr as $appinfo)
	{
		$ranking++;
		$app_info_insertdb = array();
		
		$appinfo_data = $appinfo;
		
//		if(($appbigtype == "paihang") || (($appbigtype == "sheji")))
//		{
//			//这两类结构少了app
//			$appinfo_data = $appinfo;
//		}

		if((($appbigtype == "soft") || ($appbigtype == "game")) && ($catchtype >= 100))
		{
			//这两类多了->app
			$appinfo_data = $appinfo->app;
		}

		$app_info_insertdb['name'] = addslashes($appinfo_data->title);
		$app_info_insertdb['packagename'] = $appinfo_data->packageName;
		$app_info_insertdb['AppDownTimesAll'] = $appinfo_data->installedCountStr;//装机量
		$app_info_insertdb['AppDownTimes'] = getdownloadtimesall($app_info_insertdb['packagename']);//下载量
		$app_info_insertdb['AppDownTimesToday'] = getdownloadtimes($app_info_insertdb['packagename']);
		$app_info_insertdb['ScoreType'] = $appinfo_data->likesRate;//%的人喜欢
		$app_info_insertdb['Rating'] = @"";//$appinfo->rating;
		$app_info_insertdb['AppVersion'] = $appinfo_data->apks[0]->versionName;
		$app_info_insertdb['Versioncode'] = $appinfo_data->apks[0]->versionCode;
		$app_info_insertdb['AppBaikeName'] = @"";//$appinfo->baike_name;
		$app_info_insertdb['AppLogo'] = $appinfo_data->icons->px78;
		$app_info_insertdb['AppSize'] = $appinfo_data->apks[0]->bytes;
		
		
		$app_info_insertdb['AppType'] = "";
//		if($catchtype <= 100)
		{
			$app_info_insertdb['AppType'] = $catchtype;
		}
		$app_info_insertdb['AppBigType'] = $appbigtype;
		$app_info_insertdb['AppRanking'] = $ranking;
		$app_info_insertdb['AppRankingType'] = $catchtype;
		
		$app_info_insertdb_arr[] = $app_info_insertdb;
	}
	
	return $app_info_insertdb_arr;
}

function getdownloadtimesall($packagename)
{
	// 新建一个Dom实例 
	$html = new simple_html_dom();
	$detailurl = "http://apps.wandoujia.com/apps/$packagename/dashboard?model=";
	// 从url中加载
	@$html = file_get_html($detailurl);
	if($html == "")
	{
		@$html = file_get_html($apps_url_app_all_list);
		if($html == "")
		{
			return 0;
		}
	}
	
	$appdowntimesall = $html->find('div.tongji-container section.content-container div.data-container div.install',0)->outertext;
	$appdowntimesall = getdatafromstring($appdowntimesall,"data-count");	
	
	if($appdowntimesall == "")
	{
		$appdowntimesall = 0;
	}
	$html->clear();
	return $appdowntimesall;
}


function getdownloadtimes($packagename)
{
	$downloadtime = 0;
	$downloadtimesurl = "http://index.wandoujia.com/muce-index/apps/statistics/downloads/$packagename?days=3";
	@$json_string = file_get_html($downloadtimesurl);
	$obj = json_decode($json_string);
	$downloadtimearr = $obj->data;
	if(count($downloadtimearr) == 0)
	{
		$json_string = "";
		return $downloadtime;
	}
	
	foreach ($downloadtimearr as $downloadtimeoneday)
	{
		$todaydate = strtotime(date("Ymd"));
		if($downloadtimeoneday->date == date("Ymd",mktime(0,0,0,date("m"),date("d")-1,date("Y"))))
		{
			$downloadtime = $downloadtimeoneday->downloads;
			$json_string = "";
			return $downloadtime;
		}
	}
	$json_string = "";
	return $downloadtime;
}


function get_exit_packagename_fromdb()
{
	$link = mysql_connect("localhost","root",DBPAS); 
	$createtime = strtotime(date("Y-m-d H:i:s"));

	$sql = 'select id, packageName,title from jk_apps_info_wandoujia where IsDeleted=0 '; 
	$rs = mysql_query($sql, $link);
	
	$app_exit_packagename = array();
  	while($row=mysql_fetch_array($rs))        
  	{
	  	$app_exit_packagename[] = $row['packageName'];
  	}
	return $app_exit_packagename;
}

function get_exit_appinfo_fromdb()
{
	$link = mysql_connect("localhost","root",DBPAS); 
	$createtime = strtotime(date("Y-m-d H:i:s"));

	$sql = 'select id,app_id, packageName,title,apptype,appbigtype from jk_apps_info_wandoujia where IsDeleted=0 '; 
	$rs = mysql_query($sql, $link);
	
	$app_exit_info = array();
  	while($row=mysql_fetch_array($rs))        
  	{
  		$app_exit_info_temp = array();
  		$app_exit_info_temp['id'] = $row['id'];
  		$app_exit_info_temp['app_id'] = $row['app_id'];
	  	$app_exit_info_temp['AppPackageName'] = $row['packageName'];
	  	$app_exit_info_temp['AppName'] = $row['title'];
	  	$app_exit_info_temp['apptype'] = $row['apptype'];
	  	$app_exit_info_temp['appbigtype'] = $row['appbigtype'];
	  	$app_exit_info[] = $app_exit_info_temp;
  	}
	return $app_exit_info;
}

function get_exit_appversion_fromdb()
{
	$link = mysql_connect("localhost","root",DBPAS); 
	$createtime = strtotime(date("Y-m-d H:i:s"));

	$sql = 'select id,app_id,versionName,versionCode from jk_apps_version_wandoujia where IsDeleted=0 order by id asc '; 
	$rs = mysql_query($sql, $link);
	
	$app_exit_version = array();
  	while($row=mysql_fetch_array($rs))        
  	{
  		$app_exit_version_temp = array();
	  	$app_exit_version_temp['id'] = $row['id'];
	  	$app_exit_version_temp['app_id'] = $row['app_id'];
	  	$app_exit_version_temp['versionName'] = $row['versionName'];
	  	$app_exit_version_temp['versionCode'] = $row['versionCode'];
	  	$app_exit_version[] = $app_exit_version_temp;
  	}
	return $app_exit_version;
}


function get_today_data()
{
	$today = date("Y-m-d");
	$link = mysql_connect("localhost","root",DBPAS); 
	
	$sql = 'select id, app_id,rank,ranktype,time from jk_apps_data_mobile_wandoujia_'.date('Ym',time()).' where IsDeleted=0 and time="'.$today.'"'; 
	$rs = mysql_query($sql, $link);
	
	$app_exit_info = array();
  	while($row=mysql_fetch_array($rs))        
  	{
  		$app_exit_info_temp = array();
  		$app_exit_info_temp['id'] = $row['id'];
  		$app_exit_info_temp['app_id'] = $row['app_id'];
  		$app_exit_info_temp['rank'] = $row['rank'];
	  	$app_exit_info_temp['ranktype'] = $row['ranktype'];
	  	$app_exit_info_temp['time'] = $row['time'];
	  	$app_exit_info[] = $app_exit_info_temp;
  	}
	return $app_exit_info;
}
function get_today_score()
{
	$link = mysql_connect("localhost","root",DBPAS); 
	$today = date("Y-m-d");

	$sql = 'select id, app_id from jk_apps_score_data_mobile_wandoujia_'.date('Ym',time()).' where IsDeleted=0 and time="'.$today.'"'; 
	$rs = mysql_query($sql, $link);
	
	$app_exit_info = array();
  	while($row=mysql_fetch_array($rs))        
  	{
  		$app_exit_info_temp = array();
  		$app_exit_info_temp['id'] = $row['id'];
	  	$app_exit_info_temp['app_id'] = $row['app_id'];
	  	$app_exit_info[] = $app_exit_info_temp;
  	}
	return $app_exit_info;
}




//app基础信息
function insert_appinfo_ifnotexit_todb($app_info)
{
	$link = mysql_connect("localhost","root",DBPAS); 
	$createtime = strtotime(date("Y-m-d H:i:s"));

	$sql = 'insert into jk_apps_info_wandoujia (title,packageName,url,icon,apptype,appbigtype)
	 values ("'.$app_info['name'].'","'.$app_info['packagename'].'","'.WEBURLHEADER.$app_info['packagename'].'","'.$app_info['AppLogo'].'","'.$app_info['AppType'].'","'.$app_info['AppBigType'].'")'; 

	$rs = mysql_query($sql, $link);
	//echo $sql;die();
	$filename_success = LOGPATHHEADER.'logs/wandoujia_info_success.log';
	$filename_fail = LOGPATHHEADER.'logs/wandoujia_info_fail.log';
	$filename_db_fail = LOGPATHHEADER.'logs/wandoujia_info_db_fail.log';

	try
	{
		if(!$rs)
		{
			//记录错误，二次抓取用
			$info_str = $app_info["name"].$sql."Valid result!\r\n";
			$fh = fopen($filename_db_fail, "a");
			fwrite($fh, $info_str);
			fclose($fh);
			echo $info_str;
		}
		else
		{
			if(LOGFORSUCCESS)
			{
				$info_str = $app_info["name"]."success!\r\n";
				$filename = $filename_success;
				$fh = fopen($filename, "a");
				fwrite($fh, $info_str);
				fclose($fh);
			}
		}
	}
	catch(Exception $e)
	{
		
	}
	
	return mysql_insert_id();
}
//app version信息
function insert_appversion_ifnotexit_todb($app_info)
{
	$link = mysql_connect("localhost","root",DBPAS); 
	$createtime = strtotime(date("Y-m-d H:i:s"));

	$sql = 'insert into jk_apps_version_wandoujia (app_id,versionName,versionCode)
	 values ('.$app_info['app_id'].',"'.$app_info['AppVersion'].'","'.$app_info['Versioncode'].'")';

	$rs = mysql_query($sql, $link);
	//echo $sql;die();
	$filename_success = LOGPATHHEADER.'logs/wandoujia_info_success.log';
	$filename_fail = LOGPATHHEADER.'logs/wandoujia_info_fail.log';
	$filename_db_fail = LOGPATHHEADER.'logs/wandoujia_info_db_fail.log';

	try
	{
		if(!$rs)
		{
			//记录错误，二次抓取用
			$info_str = $app_info["name"].$sql."Valid result!\r\n";
			$fh = fopen($filename_db_fail, "a");
			fwrite($fh, $info_str);
			fclose($fh);
			echo $info_str;
		}
		else
		{
			if(LOGFORSUCCESS)
			{
				$info_str = $app_info["name"]."success!\r\n";
				$filename = $filename_success;
				$fh = fopen($filename, "a");
				fwrite($fh, $info_str);
				fclose($fh);
			}
		}
	}
	catch(Exception $e)
	{
		
	}
	return mysql_insert_id();
}
//排名信息
function update_appinfo_todb($app_info_arr)
{
	$link = mysql_connect("localhost","root",DBPAS); 
	$createtime = strtotime(date("Y-m-d H:i:s"));
	
	$today = date("Y-m-d");
	
	if(count($app_info_arr) == 0)
	{
		return ;
	}
	
	$sql = 'insert into jk_apps_data_mobile_wandoujia_'.date('Ym',time()).' (app_id,app_version_id,rank,ranktype,time) values '; 

	$i=0;
	foreach ($app_info_arr as $app_info)
	{
		if($i==0)
		{
			$sql .= '('.$app_info['app_id'].','.$app_info['app_version_id'].','.$app_info['AppRanking'].','.$app_info['AppRankingType'].',"'.$today.'")';
		}
		else
		{
			$sql .= ',('.$app_info['app_id'].','.$app_info['app_version_id'].','.$app_info['AppRanking'].','.$app_info['AppRankingType'].',"'.$today.'")';
		}
		$i++;
	}
	
	
//	$sql = 'insert into jk_apps_data_mobile_wandoujia_'.date('Ym',time()).' (app_id,app_version_id,rank,ranktype,time) values ('.$app_info['app_id'].','.$app_info['app_version_id'].','.$app_info['AppRanking'].','.$app_info['AppRankingType'].',"'.$today.'")'; 

//	echo $sql;die();
	$rs = mysql_query($sql, $link);
	
	$filename_success = LOGPATHHEADER.'logs/wandoujia_success.log';
	$filename_fail = LOGPATHHEADER.'logs/wandoujia_fail.log';
	$filename_db_fail = LOGPATHHEADER.'logs/wandoujia_db_fail.log';

	try
	{
		if(!$rs)
		{
			//记录错误，二次抓取用
			$info_str = $sql."Valid result!\r\n";
			$fh = fopen($filename_db_fail, "a");
			fwrite($fh, $info_str);
			fclose($fh);
			echo $info_str;
		}
		else
		{
			if(LOGFORSUCCESS)
			{
				$info_str = $app_info["name"]."success!\r\n";
				$filename = $filename_success;
				$fh = fopen($filename, "a");
				fwrite($fh, $info_str);
				fclose($fh);
			}
		}
	}
	catch(Exception $e)
	{
		
	}
}

function getcommentcount($apppackagename)
{
	$catchcommentcounturl = "http://comment.wandoujia.com/comment/comment!getCommentSummary.action?target=$apppackagename&pageSize=10&f=phoenix2&v=3.24.1&vc=3918&u=b19e8780415b44108c0c7f4014898b7b938fd64b&ch=wandoujia_wap&pageNum=0";
	
	@$json_string = file_get_html($catchcommentcounturl);
	$obj=json_decode($json_string);
	//echo $obj->data->total;die();
	return $obj->count;
}

//监控信息
function update_appscore_todb($app_info_arr)
{
	$link = mysql_connect("localhost","root",DBPAS); 
	$createtime = strtotime(date("Y-m-d H:i:s"));
	
	$today = date("Y-m-d");
	
	if(count($app_info_arr) == 0)
	{
		return ;
	}
	
	$sql = 'insert into jk_apps_score_data_mobile_wandoujia_'.date('Ym',time()).' (app_id,app_version_id,downloads,downtimestoday,downtimesall,score,scoretype,comments,time) values '; 

	$i=0;
	foreach ($app_info_arr as $app_info)
	{
		if(strstr($app_info['AppDownTimes'],"万"))
		{
			$app_info['AppDownTimes'] = str_replace("万","",$app_info['AppDownTimes']) * 10000;
		}
		if(strstr($app_info['AppDownTimes'],"亿"))
		{
			$app_info['AppDownTimes'] = str_replace("亿","",$app_info['AppDownTimes']) * 100000000;
		}
		$app_info['AppDownTimes'] = intval($app_info['AppDownTimes']);
		if(strstr($app_info['AppDownTimesToday'],"万"))
		{
			$app_info['AppDownTimesToday'] = str_replace("万","",$app_info['AppDownTimesToday']) * 10000;
		}
		if(strstr($app_info['AppDownTimesToday'],"亿"))
		{
			$app_info['AppDownTimesToday'] = str_replace("亿","",$app_info['AppDownTimesToday']) * 100000000;
		}
		
		
		if(strstr($app_info['AppDownTimesAll'],"万"))
		{
			$app_info['AppDownTimesAll'] = str_replace("万","",$app_info['AppDownTimesAll']) * 10000;
		}
		if(strstr($app_info['AppDownTimesAll'],"亿"))
		{
			$app_info['AppDownTimesAll'] = str_replace("亿","",$app_info['AppDownTimesAll']) * 100000000;
		}
		$app_info['AppDownTimesAll'] = intval($app_info['AppDownTimesAll']);
	
		if($app_info['Rating'] == "")
		{
			$app_info['Rating'] = 0;
		}
		
		$commentcount = getcommentcount($app_info['packagename']);
		if($commentcount == "")
		{
			$commentcount = 0;
		}
		
		if($i==0)
		{
			$sql .= ' ('.$app_info['app_id'].','.$app_info['app_version_id'].','.$app_info['AppDownTimes'].','.$app_info['AppDownTimesToday'].','.$app_info['AppDownTimesAll'].','.$app_info['Rating'].',"'.$app_info['ScoreType'].'",'.$commentcount.',"'.$today.'")';
		}
		else
		{
			$sql .= ', ('.$app_info['app_id'].','.$app_info['app_version_id'].','.$app_info['AppDownTimes'].','.$app_info['AppDownTimesToday'].','.$app_info['AppDownTimesAll'].','.$app_info['Rating'].',"'.$app_info['ScoreType'].'",'.$commentcount.',"'.$today.'")';
		}
		$i++;
	}
	
	
//	$sql = 'insert into jk_apps_score_data_mobile_wandoujia_'.date('Ym',time()).' (app_id,app_version_id,downloads,score,scoretype,comments,time) values ('.$app_info['app_id'].','.$app_info['app_version_id'].','.$app_info['AppDownTimes'].','.$app_info['Rating'].',"'.$app_info['ScoreType'].'",'.$commentcount.',"'.$today.'")'; 

//	echo $sql;die();
	$rs = mysql_query($sql, $link);
	
	$filename_success = LOGPATHHEADER.'logs/wandoujia_success.log';
	$filename_fail = LOGPATHHEADER.'logs/wandoujia_fail.log';
	$filename_db_fail = LOGPATHHEADER.'logs/wandoujia_db_fail.log';

	try
	{
		if(!$rs)
		{
			//记录错误，二次抓取用
			$info_str = $sql."Valid result!\r\n";
			$fh = fopen($filename_db_fail, "a");
			fwrite($fh, $info_str);
			fclose($fh);
			echo $info_str;
		}
		else
		{
			if(LOGFORSUCCESS)
			{
				$info_str = $app_info["name"]."success!\r\n";
				$filename = $filename_success;
				$fh = fopen($filename, "a");
				fwrite($fh, $info_str);
				fclose($fh);
			}
		}
	}
	catch(Exception $e)
	{
		
	}
}







function getcomment()
{
	//获取已存在app基础数据
	
	$app_exit_infos = get_exit_appinfo_fromdb();
	foreach ($app_exit_infos as $app_exit)
	{
		$apppackagename =  $app_exit['AppPackageName'];
		
		decodeurlandgetcomment($app_exit,"http://comment.wandoujia.com/comment/comment!getCommentSummary.action?target=$apppackagename&pageSize=10&f=phoenix2&v=3.24.1&vc=3918&u=b19e8780415b44108c0c7f4014898b7b938fd64b&ch=wandoujia_wap&pageNum=");
	}
}


function decodeurlandgetcomment($app_exit,$urlheader)
{
	$app_exit_infos = get_exit_appinfo_fromdb();
	$app_exit_versions = get_exit_appversion_fromdb();
	$app_exit_comment = get_exit_appcomment_fromdb();
	
	for($i=0;;$i++)
	{
		$apps_url_app_all_list = $urlheader.$i;
	    
		@$json_string = file_get_html($apps_url_app_all_list);
		$obj = json_decode($json_string);
		
		$appinfoarr = $obj->comments;
		
		//本次为空，结束
		if((count($appinfoarr) == 0) || ($appinfoarr == ""))
		{
			break;
		}
		
		//有内容，解析
		$app_info_arr_insertdb_arr = catchcommentdata($appinfoarr);
		
		
		//写数据库
		foreach ($app_info_arr_insertdb_arr as $app_info_arr_insertdb)
		{
			$app_info_arr_insertdb['app_id'] = $app_exit['id'];
			$app_info_arr_insertdb['app_version_id'] = 0;
			foreach ($app_exit_versions as $app_versions)
			{
				if($app_versions['app_id'] == $app_info_arr_insertdb['app_id'])
				{
					$app_info_arr_insertdb['app_version_id'] = $app_versions['id'];
					break;
				}
			}
			//判断是否需要更新信息库
			$searchcommentid = searchcommentisexit($app_info_arr_insertdb, $app_exit_comment);
			if($searchcommentid == 0)
			{
				update_appcomment_todb($app_info_arr_insertdb,0);
				//_ifnotexit_todb($app_info_arr_insertdb);
				
				//重新获取存在app信息，因为有了更新
				$app_exit_comment = get_exit_appcomment_fromdb();
			}
		}
		
	}
}

//排名分类，app大类，app信息
function catchcommentdata($appcommentarr)
{
	$app_info_insertdb_arr = array();
	
	foreach ($appcommentarr as $appcomment)
	{
		$app_info_insertdb = array();
		$appcomment_data = $appcomment;

		$app_info_insertdb['id'] = $appcomment_data->id;
		$app_info_insertdb['content'] = addslashes($appcomment_data->content);
		$app_info_insertdb['date'] = $appcomment_data->date;
		$app_info_insertdb['author_name'] = addslashes($appcomment_data->authorName);
		$app_info_insertdb['author_id'] = $appcomment_data->authorId;
		$app_info_insertdb_arr[] = $app_info_insertdb;
	}
	return $app_info_insertdb_arr;
}

//监控信息
function update_appcomment_todb($app_comment,$platform_id)
{
	$link = mysql_connect("localhost","root",DBPAS); 
	$createtime = strtotime(date("Y-m-d H:i:s"));
	
	//$datano = date("Y-m-d",strtotime($app_comment["date"]));
	
	$sql = 'insert into jk_apps_comments_detail_wandoujia (app_id,app_version_id,platform_id,author_id,author_name,content,time)
	 values ('.$app_comment['app_id'].','.$app_comment['app_version_id'].','.$platform_id.',"'.$app_comment['author_id'].'","'.$app_comment['author_name'].'","'.$app_comment['content'].'","'.$app_comment['date'].'")'; 

//	echo $sql;die();
	$rs = mysql_query($sql, $link);
	
	$filename_success = LOGPATHHEADER.'logs/wandoujia_success.log';
	$filename_fail = LOGPATHHEADER.'logs/wandoujia_fail.log';
	$filename_db_fail = LOGPATHHEADER.'logs/wandoujia_db_fail.log';

	try
	{
		if(!$rs)
		{
			//记录错误，二次抓取用
			$info_str = $app_comment["app_id"].$sql."Valid result!\r\n";
			$fh = fopen($filename_db_fail, "a");
			fwrite($fh, $info_str);
			fclose($fh);
			echo $info_str;
		}
		else
		{
			if(LOGFORSUCCESS)
			{
				$info_str = $app_comment["app_id"]."success!\r\n";
				$filename = $filename_success;
				$fh = fopen($filename, "a");
				fwrite($fh, $info_str);
				fclose($fh);
			}
		}
	}
	catch(Exception $e)
	{
		
	}
}



function get_exit_appcomment_fromdb()
{
	$link = mysql_connect("localhost","root",DBPAS); 
	
	$sql = 'select id,app_id,app_version_id,author_id,content,time from jk_apps_comments_detail_wandoujia where IsDeleted=0 '; 
	$rs = mysql_query($sql, $link);
	
	$app_exit_comment = array();
  	while($row=mysql_fetch_array($rs))        
  	{
  		$app_exit_comment_temp = array();
	  	$app_exit_comment_temp['id'] = $row['id'];
	  	$app_exit_comment_temp['app_id'] = $row['app_id'];
	  	$app_exit_comment_temp['app_version_id'] = $row['app_version_id'];
	  	$app_exit_comment_temp['author_id'] = $row['author_id'];
	  	$app_exit_comment_temp['content'] = $row['content'];
	  	$app_exit_comment_temp['time'] = $row['time'];
	  	$app_exit_comment[] = $app_exit_comment_temp;
  	}
	return $app_exit_comment;
}

function searchcommentisexit($app_comment, $app_exit_comment)
{
	foreach ($app_exit_comment as $app_exit) 
	{
		if (($app_comment['app_id'] == $app_exit["app_id"]) && ($app_comment['app_version_id'] == $app_exit["app_version_id"]) && ($app_comment['author_id'] == $app_exit["author_id"]) && ($app_comment['content'] == $app_exit["content"]) && ($app_comment['date'] == $app_exit["time"])) 
		{
			return $app_exit['id'];
		}
	}
	return 0;
}











function getrankfrompc()
{
	//获取已存在app基础数据
	
//	应用分类
//	$apptype_arr = getapptypelistfrompc();
//	foreach ($apptype_arr as $apptype)
//	{
//		每一类的url
//		$apps_url_app_type_list = $apptype['url'].urlencode($apptype['name'])."?navType=app&pos=w/tag/appnav";
//		decodeurlandgetdataforpc($apptype['id'],"soft",$apps_url_app_type_list);
//	}
//	应用总榜
//	decodeurlandgetdataforpc(101,"soft","http://apps.wandoujia.com/tag/".urlencode("全部软件")."?navType=app&pos=w/tag/appnav");
	
	//游戏分类
	$gametype_arr = getgametypelistfrompc();
	foreach ($gametype_arr as $gametype)
	{
		//每一类的url
		$apps_url_game_type_list = $gametype['url'].urlencode($gametype['name'])."?navType=game&pos=w/tag/gamenav";
		writealllog($apps_url_game_type_list);
		decodeurlandgetdataforpc($gametype['id'],"game",$apps_url_game_type_list);
	}
	//游戏总榜
	decodeurlandgetdataforpc(102,"game","http://apps.wandoujia.com/tag/".urlencode("全部游戏")."?navType=game&pos=w/tag/gamenav");

	//豌豆荚排行榜
//	decodeurlandgetdataforpc(111,"paihang","");
	
	//设计奖
//	decodeurlandgetdataforpc(112,"sheji","");

}



function decodeurlandgetdataforpc($catchtype,$appbigtype,$urlheader)
{
	$app_exit_infos = get_exit_appinfo_fromdb();
	$app_exit_versions = get_exit_appversion_fromdb();

	$app_today_data = get_today_data();
	$app_today_score = get_today_score();
	
    $app_exit_packagename = get_exit_packagename_fromdb();
	
	for($i=1;;)//$i<=20
	{
	    $apps_url_app_all_list = $urlheader."&page=".$i;
	    //echo $apps_url_app_all_list;die();
	    $packagenamearr = getpackagenamearrfromlist($apps_url_app_all_list);
	    
	    if((count($packagenamearr) == 0) || ($packagenamearr == ""))
	    {
	    	break;
	    }
	    
	    $rankpos = ($i-1) * 72;
	    $app_info_arr_insertdb_data = array();
	    $appdetail_info_insertdb_arr = array();
	    foreach ($packagenamearr as $packagename)
	    {
	    	$rankpos++;
	    	if(in_array($packagename,$app_exit_packagename))
	    	{
	    		//if(searchisexittodaydata($app_info_arr_insertdb, $app_today_data) == 0)
				{
					$app_info_arr_insertdb['app_id'] = 0;
					$app_info_arr_insertdb['app_version_id'] = 0;
					foreach ($app_exit_infos as $app_exit_info)
					{
						if($packagename == $app_exit_info['AppPackageName'])
						{
							$app_info_arr_insertdb['app_id'] = $app_exit_info['id'];
							break;
						}
					}
					if($app_info_arr_insertdb['app_id'] != 0)
					{
						foreach ($app_exit_versions as $app_exit_version)
						{
							if($app_info_arr_insertdb['app_id'] == $app_exit_version['app_id'])
							{
								$app_info_arr_insertdb['app_version_id'] = $app_exit_version['id'];
							}
						}
					}
					if($app_info_arr_insertdb['app_id'] != 0)
					{
						$app_info_arr_insertdb['AppRanking'] = $rankpos;
						$app_info_arr_insertdb['AppRankingType'] = $catchtype;
						$app_info_arr_insertdb_data[] = $app_info_arr_insertdb;
					}
					
				}
	    	}
	    	else
	    	{
	    		$wapurl_footer = ".html";
				$wapurl = WAPURLHEADERWANDOUJIA.$packagename.WAPURLFOOTERWANDOUJIA;
				$appdetail_info_arr = getappdetailinfowandoujia($packagename,$wapurl,$catchtype,$appbigtype);
				$appdetail_info_arr['AppRanking'] = $rankpos;
				
				$appdetail_info_insertdb_arr[] = $appdetail_info_arr;
	    	}
	    }
	    writealllog("db");
	    update_apprankpc_todb($app_info_arr_insertdb_data);
	    insertintodbpc($appdetail_info_insertdb_arr);
	    $i++;
	}
}

function getpackagenamearrfromlist($urllist)
{
	writealllog($urllist."getpackagename");
	$app_packagenam_arr = array();
	
//	$ranking = $flag ;//* ONCECATCHCOUNT;
	$app_info_insertdb_arr = array();
	
	// 新建一个Dom实例 
	$html = new simple_html_dom();
	// 从url中加载
	@$html = file_get_html($urllist);
	if($html == "")
	{
		@$html = file_get_html($urllist);
		if($html == "")
		{
			return $app_info_insertdb_arr;
		}
	}

	$appinfoarr = $html->find('div.app-block-wp li.app-item div.app ');
	
	foreach ($appinfoarr as $appinfotemp)
	{
		$appinfostr = $appinfotemp->outertext;
		$packagename = getdatafromstring($appinfostr,"data-pn");
		$app_packagenam_arr[] = $packagename;
		writefailelog(1,$packagename);
		writealllog($packagename);
	}
	$html->clear();
	return $app_packagenam_arr;
}

function getdatafromstring($appinfostr,$tagname)
{
	$pos = strpos($appinfostr, $tagname);
	$appinfostr_tmp = substr($appinfostr,$pos,(strlen($appinfostr)-$pos));
	$pos = strpos($appinfostr_tmp, "\"");
	$appinfostr_tmp = substr($appinfostr_tmp,$pos+1,(strlen($appinfostr)-$pos));
	$pos = strpos($appinfostr_tmp, "\"");
	$appinfostr_tmp = substr($appinfostr_tmp,0,$pos);
	return $appinfostr_tmp;
}


//排名信息
function update_apprankpc_todb($app_info_arr)
{
	$link = mysql_connect("localhost","root",DBPAS); 
	$createtime = strtotime(date("Y-m-d H:i:s"));
	
	$today = date("Y-m-d");
	if(count($app_info_arr) == 0)
	{
		return ;
	}
	
	$sql = 'insert into jk_apps_data_pc_wandoujia_'.date('Ym',time()).' (app_id,app_version_id,rank,ranktype,time) values '; 

	$i=0;
	foreach ($app_info_arr as $app_info)
	{
		if($i==0)
		{
			$sql .= '('.$app_info['app_id'].','.$app_info['app_version_id'].','.$app_info['AppRanking'].','.$app_info['AppRankingType'].',"'.$today.'")';
		}
		else
		{
			$sql .= ',('.$app_info['app_id'].','.$app_info['app_version_id'].','.$app_info['AppRanking'].','.$app_info['AppRankingType'].',"'.$today.'")';
		}
		$i++;
	}
	writealllog($sql);
	$rs = mysql_query($sql, $link);
	
	$filename_success = LOGPATHHEADER.'logs/wandoujia_success.log';
	$filename_fail = LOGPATHHEADER.'logs/wandoujia_fail.log';
	$filename_db_fail = LOGPATHHEADER.'logs/wandoujia_db_fail.log';

	try
	{
		if(!$rs)
		{
			//记录错误，二次抓取用
			$info_str = $sql."Valid result!\r\n";
			$fh = fopen($filename_db_fail, "a");
			fwrite($fh, $info_str);
			fclose($fh);
			echo $info_str;
		}
		else
		{
			if(LOGFORSUCCESS)
			{
				$info_str = $app_info["name"]."success!\r\n";
				$filename = $filename_success;
				$fh = fopen($filename, "a");
				fwrite($fh, $info_str);
				fclose($fh);
			}
		}
	}
	catch(Exception $e)
	{
		
	}
}


function getappdetailinfowandoujia($packagename,$app_detail_url,$catchtype,$appbigtype)
{
	$appdetailinfo_arr = array();
	
	@$json_string = file_get_html($app_detail_url);
	$appinfo_data = json_decode($json_string);
	//本次为空，结束
	if((count($appinfo_data) == 0) || ($appinfo_data == ""))
	{
		return $appdetailinfo_arr;
	}

	$appdetailinfo_arr['name'] = addslashes($appinfo_data->title);
	$appdetailinfo_arr['packagename'] = $packagename;//$appinfo_data->packageName;
	$appdetailinfo_arr['AppDownTimesAll'] = $appinfo_data->installedCountStr;
	$appdetailinfo_arr['AppDownTimes'] = getdownloadtimesall($appdetailinfo_arr['packagename']);
	$appdetailinfo_arr['AppDownTimesToday'] = getdownloadtimes($packagename);//getdownloadtimes($app_info_insertdb['packagename']);
	if($appdetailinfo_arr['AppDownTimesToday'] == "")
	{
		$appdetailinfo_arr['AppDownTimesToday'] = 0;
	}
	$appdetailinfo_arr['ScoreType'] = $appinfo_data->likesRate;//%的人喜欢
	$appdetailinfo_arr['Rating'] = @"";//$appinfo->rating;
	$appdetailinfo_arr['AppVersion'] = $appinfo_data->apks[0]->versionName;
	$appdetailinfo_arr['Versioncode'] = $appinfo_data->apks[0]->versionCode;
	$appdetailinfo_arr['AppBaikeName'] = @"";//$appinfo->baike_name;
	$appdetailinfo_arr['AppLogo'] = $appinfo_data->icons->px78;
	$appdetailinfo_arr['AppSize'] = $appinfo_data->apks[0]->bytes;
	$appdetailinfo_arr['CommentsCount'] = $appinfo_data->commentsCount;
	
	$appdetailinfo_arr['AppType'] = $catchtype;
	$appdetailinfo_arr['AppBigType'] = $appbigtype;
	$appdetailinfo_arr['AppRanking'] = 0;
	$appdetailinfo_arr['AppRankingType'] = $catchtype;
	
	return $appdetailinfo_arr;
}

function insertintodbpc($app_info_arr_insertdb_arr)
{writealllog("insert");
	$app_exit_infos = get_exit_appinfo_fromdb();
	$app_exit_versions = get_exit_appversion_fromdb();
	
	$app_today_data = get_today_data();
	$app_today_score = get_today_score();
	
	$app_info_arr_insertdb['app_id'] = 0;
	$app_info_arr_insertdb['app_version_id'] = 0;
	
	$app_info_arr_insertdb_data = array();
	$app_info_arr_insertdb_scoredata = array();
	//写数据库
	foreach ($app_info_arr_insertdb_arr as $app_info_arr_insertdb)
	{
		//判断是否需要更新信息库
		$searchinfoid = searchisexit($app_info_arr_insertdb, $app_exit_infos);
		if($searchinfoid == 0)
		{
			$app_info_arr_insertdb['app_id'] = insert_appinfo_ifnotexit_todb($app_info_arr_insertdb);
			$app_info_arr_insertdb['app_version_id'] = insert_appversion_ifnotexit_todb($app_info_arr_insertdb);
			
			//重新获取存在app信息，因为有了更新
			$app_exit_infos = get_exit_appinfo_fromdb();
			$app_exit_versions = get_exit_appversion_fromdb();
		}
		else 
		{
			$app_info_arr_insertdb['app_id'] = $searchinfoid;
			$searchversionid = searchisexitversion($app_info_arr_insertdb, $app_exit_versions);
			if($searchversionid == 0)
			{
				$app_info_arr_insertdb['app_version_id'] = insert_appversion_ifnotexit_todb($app_info_arr_insertdb);
				
				//重新获取存在app信息，因为有了更新
				$app_exit_versions = get_exit_appversion_fromdb();
			}
			else 
			{
				$app_info_arr_insertdb['app_version_id'] = $searchversionid;
			}
		}
		//判断当天没有抓过，再写入
		if(searchisexittodaydata($app_info_arr_insertdb, $app_today_data) == 0)
		{
			//update_appinfo_todb($app_info_arr_insertdb);
			//$app_today_data = get_today_data();
			$app_info_arr_insertdb_data[] = $app_info_arr_insertdb;
		}
		if(searchisexittodayscore($app_info_arr_insertdb, $app_today_score) == 0)
		{
			//update_appscore_todb($app_info_arr_insertdb);
			//$app_today_score = get_today_score();
			$app_info_arr_insertdb_scoredata[] = $app_info_arr_insertdb;
		}

	}
	
	//update_appinfo_todb($app_info_arr_insertdb_data);
	update_apprankpc_todb($app_info_arr_insertdb_data);
	update_appscore_todb($app_info_arr_insertdb_scoredata);
}


function catchother()
{
	$app_exit_infos = get_exit_appinfo_fromdb_forcatch();
	$app_exit_versions = get_exit_appversion_fromdb();
	
	$app_today_data = get_today_data();
	$app_today_score = get_today_score();
	
	$app_today_score_id_arr = array();
	foreach ($app_today_score as $app_today_score_data)
	{
		$app_today_score_id_arr[] = $app_today_score_data["app_id"];
	}
	
	foreach ($app_exit_infos as $app_exit_info)
	{
		if(in_array($app_exit_info['id'],$app_today_score_id_arr))
		{//已抓
			continue;
		}
		//未抓
		$packagename = $app_exit_info['AppPackageName'];
		if($packagename == "")
		{
			continue;
		}
		$wapurl = WAPURLHEADERWANDOUJIA.$packagename.WAPURLFOOTERWANDOUJIA;
		$app_info_arr_insertdb = getappdetailinfowandoujia($packagename,$wapurl,$app_exit_info['apptype'],$app_exit_info['appbigtype']);
		if(count($app_info_arr_insertdb) == 0)
		{
			continue;
		}
		
		$app_info_arr_insertdb['AppRanking'] = 0;//这里抓的没有排名
		$app_info_arr_insertdb['AppRankingType'] = $app_exit_info['apptype'];
		
		$app_info_arr_insertdb['app_id'] = 0;
		$app_info_arr_insertdb['app_version_id'] = 0;
		
		//判断是否需要更新信息库
		$searchinfoid = searchisexit($app_info_arr_insertdb, $app_exit_infos);
		if($searchinfoid == 0)
		{
			//理论上不可能不存在，因为是从这个数组中取的
			
//			$app_info_arr_insertdb['app_id'] = insert_appinfo_ifnotexit_todb($app_info_arr_insertdb);
//			$app_info_arr_insertdb['app_version_id'] = insert_appversion_ifnotexit_todb($app_info_arr_insertdb);
//			
//			//重新获取存在app信息，因为有了更新
//			$app_exit_infos = get_exit_appinfo_fromdb();
//			$app_exit_versions = get_exit_appversion_fromdb();
		}
		else 
		{
			$app_info_arr_insertdb['app_id'] = $searchinfoid;
			$searchversionid = searchisexitversion($app_info_arr_insertdb, $app_exit_versions);
			if($searchversionid == 0)
			{
				//理论上不可能不存在，因为是从这个数组中取的
				
//				$app_info_arr_insertdb['app_version_id'] = insert_appversion_ifnotexit_todb($app_info_arr_insertdb);
//				
//				//重新获取存在app信息，因为有了更新
//				$app_exit_versions = get_exit_appversion_fromdb();
			}
			else 
			{
				$app_info_arr_insertdb['app_version_id'] = $searchversionid;
			}
		}
		//判断当天没有抓过，再写入
		if(searchisexittodaydata($app_info_arr_insertdb, $app_today_data) == 0)
		{
			//update_appinfo_todb($app_info_arr_insertdb);
			//$app_today_data = get_today_data();
			$app_info_arr_insertdb_data[] = $app_info_arr_insertdb;
		}
		if(searchisexittodayscore($app_info_arr_insertdb, $app_today_score) == 0)
		{
			//update_appscore_todb($app_info_arr_insertdb);
			//$app_today_score = get_today_score();
			$app_info_arr_insertdb_scoredata[] = $app_info_arr_insertdb;
		}
	
		//此处的都是排行榜之外的，因此不写排行榜之外的数据
	//		update_appinfo_todb($app_info_arr_insertdb_data);
		update_appscore_todb($app_info_arr_insertdb_scoredata);
	//		$app_today_data = get_today_data();
	//		$app_today_score = get_today_score();
		$app_info_arr_insertdb_data = array();
		$app_info_arr_insertdb_scoredata = array();
	}
}




function get_exit_appinfo_fromdb_forcatch()
{
	$link = mysql_connect("localhost","root",DBPAS); 
	$createtime = strtotime(date("Y-m-d H:i:s"));

	$sql = 'select id,app_id, packageName,title,apptype,appbigtype from jk_apps_info_wandoujia where ((apptype>=51 and apptype<100) or apptype =102) and IsDeleted=0 '; 
	$rs = mysql_query($sql, $link);
	
	$app_exit_info = array();
  	while($row=mysql_fetch_array($rs))        
  	{
  		$app_exit_info_temp = array();
  		$app_exit_info_temp['id'] = $row['id'];
  		$app_exit_info_temp['app_id'] = $row['app_id'];
	  	$app_exit_info_temp['AppPackageName'] = $row['packageName'];
	  	$app_exit_info_temp['AppName'] = $row['title'];
	  	$app_exit_info_temp['apptype'] = $row['apptype'];
	  	$app_exit_info_temp['appbigtype'] = $row['appbigtype'];
	  	$app_exit_info[] = $app_exit_info_temp;
  	}
	return $app_exit_info;
}


function writefailelog($rs,$id)
{
	$filename_update = LOGPATHHEADER.'logs/wandoujiapcgame.log';
	$time = date("Y-m-d H:i:s");
	try
	{
		//记录错误，二次抓取用
		$info_str = $id."--".$time."\r\n";
		$fh = fopen($filename_update, "a");
		fwrite($fh, $info_str);
		fclose($fh);
	}
	catch(Exception $e)
	{
		
	}
}

function writealllog($id)
{
	$filename_update = LOGPATHHEADER.'logs/wandoujiapcgame_all.log';
	$time = date("Y-m-d H:i:s");
	try
	{
		//记录错误，二次抓取用
		$info_str = $id."--".$time."\r\n";
		$fh = fopen($filename_update, "a");
		fwrite($fh, $info_str);
		fclose($fh);
	}
	catch(Exception $e)
	{
		
	}
}

?>
